package com.fcx.workai.loginmodule.ui.smslogin;/**
 * Created by hxh on 2017/8/4.
 */

import com.fcx.workai.loginmodule.BuildConfig;
import com.fcx.workai.loginmodule.R;
import com.hxh.component.basicore.component.mvp.persenter.BasePresenter;
import com.hxh.component.basicore.component.net.IApiError;
import com.hxh.component.basicore.util.Utils;
import com.hxh.component.basicore.util.rx.normalhttpstyle.NormalSubscribe;
import com.hxh.component.basicore.util.rx.resetfulhttpstyle.ProgressSubScribe;
import com.hxh.component.business.app.App;
import com.hxh.component.business.bean.OpenProgressBean;
import com.hxh.component.business.common.UserInfoBean;
import com.hxh.component.business.common.api.Request_GetVerityCode;
import com.hxh.component.business.ui.bean.UserCompanyListBean;

import com.fcx.workai.loginmodule.api.ApiFactory;
import com.fcx.workai.loginmodule.api.request.Request_RefreshToken;
import com.fcx.workai.loginmodule.api.request.Request_SmsLogin;
import com.hxh.component.business.utils.Constant;

import retrofit2.Response;
import rx.Observable;
import rx.functions.Func1;

import static com.hxh.component.business.utils.Constant.TENANT_ID;

/**
 * 创建者：hxh
 * 时间：  2017/8/4
 * 描述：
 */
public class SMSLoginPresenter extends BasePresenter<SMSLoginFragment> implements SMSLoginContract.P{


    @Override
    public void login(String acount, String veritycode, String scope) {
        addSubscription(
                ApiFactory
                .login_sms(new Request_SmsLogin(acount,veritycode,scope))
                .subscribe(new ProgressSubScribe<UserInfoBean>() {
                    @Override
                    public void _OnError(Throwable msg) {
                        getView().showLoginFaildMsg("验证码有误，请重新获取！");
                    }
                    @Override
                    public void _OnNet(UserInfoBean userInfoBean) {
                        App.getUserDTO().setUser(userInfoBean);
                        getView().showLoginResule(userInfoBean);
                    }
                }));
    }

    @Override
    public void getVerityCode(String acount, String type, String scope) {
        addSubscription(
                ApiFactory.getVerityCode(new Request_GetVerityCode(acount,type,scope))
                .subscribe(new ProgressSubScribe<Response<Void>>() {
                    @Override
                    public void _OnError(Throwable throwable) {

                    }

                    @Override
                    public void _OnNet(Response<Void> voidResponse) {
                        if(200==voidResponse.code()||204==voidResponse.code()){
                            getView().showVerifyCode();
                        }else if(404==voidResponse.code()){
                            Utils.Toast.toast("用户不存在");
                        }else{
                            Utils.Toast.toast(voidResponse.message());
                        }
                    }
                })
        );
    }

    @Override
    public void refreshToken() {
        final String mTenantId ;
        String baseUrl = com.hxh.component.business.BuildConfig.AUTH_ENDPOINT;
        if ("https://api.workai.com.cn/".equals(baseUrl) || "http://120.55.17.174/".equals(baseUrl)){
            mTenantId = Constant.TENANT_ID;
        }else {
            mTenantId = Constant.DEBUF_TENANT_ID;
        }

        addSubscription(
                ApiFactory
                        .getUserCompanyList(true,null)

                        .flatMap(new Func1<UserCompanyListBean, Observable<UserInfoBean>>() {
                            @Override
                            public Observable<UserInfoBean> call(UserCompanyListBean userCompanyListBean)
                            {
                                if(userCompanyListBean.getTotal_count() <= 0)
                                {
                                    Observable.error(new Throwable(""));
                                }
                                return ApiFactory.refreToken(new Request_RefreshToken(App.getUserDTO().getUser().getRefresh_token(),
                                        mTenantId
                                ));
                            }
                        })
                        .subscribe(new ProgressSubScribe<UserInfoBean>(true) {
                            @Override
                            public void _OnError(Throwable msg) {
                                IApiError apiError = getApiError(msg);
                                if(apiError!=null&&apiError.getErrorCode()==401){
                                    getView().showRefreshTokenResult(false);
                                }else if(apiError!=null&&apiError.getErrorCode()==400)
                                {
                                    getView().showRefreshTokenResult(false);
                                }
                            }

                            @Override
                            public void _OnNet(UserInfoBean userInfoBean) {
                                App.getUserDTO().setUser(userInfoBean);
                                getView().showRefreshTokenResult(true);
                            }
                        })
        );
    }
}
